package core.runner;

import core.solver.queue.Node;
import org.jsfml.graphics.*;
import org.jsfml.system.Clock;
import org.jsfml.system.Time;
import org.jsfml.window.VideoMode;
import org.jsfml.window.Window;
import org.jsfml.window.event.Event;
import stud.problem.pathfinding.Direction;
import stud.problem.pathfinding.Move;
import stud.problem.pathfinding.Position;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class NPuzzle_Player {
    private RenderWindow window;
    private int size;
    private List<Text> texts;
    private static Font font;

    public NPuzzle_Player(){}

    static {
        font = new Font();
        try{
            font.loadFromFile(Paths.get("C:\\Windows\\Fonts\\comic.ttf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init(){
        window = new RenderWindow(new VideoMode(1100, 800), "JSFML", Window.DEFAULT);
        window.setFramerateLimit(100);
    }

    public void update(Node node)
    {
        this.size=((Position)node.getState()).size;
        texts = new ArrayList<>();
        for(int i=0;i<size;++i){
            for(int j=0;j<size;++j){
                Text text;
                if(((Position)node.getState()).state[i][j]==0){
                    text = new Text(" ",font);
                }else {
                    text = new Text(Integer.toString(((Position) node.getState()).state[i][j]), font);
                }
                text.setCharacterSize(70);
                text.setPosition(j*150,i*150);
                texts.add(text);
            }
        }
        //display();
    }

    public void display(Deque<Node> path,String result)
    {
        Text re=new Text(result,font);
        re.setCharacterSize(33);
        re.setPosition(0,600);

        Node node = path.removeFirst();
        Clock clock=new Clock();
        Time elsaptime=Time.getSeconds(0);

        update(node);
        int[] offsets = Direction.offset(((Move) path.getFirst().getAction()).getDirection());
        int xx,yy;
        xx=((Position)node.getState()).blank[0]+offsets[0];
        yy=((Position)node.getState()).blank[1]+offsets[1];
        //当窗口打开时，进入循环
        while(window.isOpen())
        {
            Time st=clock.restart();
            elsaptime = Time.add(elsaptime,st);
            float s = elsaptime.asSeconds();

            window.clear(new Color(94,148,162));
            for(Event event : window.pollEvents())
            {
                if(event.type == Event.Type.CLOSED) {
                    System.out.println("close");
                    window.close();
                }
            }
            if(offsets!=null && !texts.get(xx*size+yy).getPosition().equals
                    (texts.get((xx-offsets[0])*size+(yy-offsets[1])).getPosition())){
                texts.get(xx*size+yy).move(-offsets[1]*5,-offsets[0]*5);
            }else{
                elsaptime=Time.getSeconds(0);
                if(!path.isEmpty()){
                    node = path.removeFirst();
                    update(node);
                    if(!path.isEmpty()) {
                        offsets = Direction.offset(((Move) path.getFirst().getAction()).getDirection());
                        xx = ((Position) node.getState()).blank[0] + offsets[0];
                        yy = ((Position) node.getState()).blank[1] + offsets[1];
                    }
                }else{
                    offsets=null;
                    texts.add(re);
                }
            }
            for(Text e :texts){
                window.draw(e);
            }
            window.display();
        }
    }
}
